package top.jfunc.network.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author xiongshiyan at 2019/5/7 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
    @RequestMapping("only")
    public String only(@RequestParam(value = "file")MultipartFile multipartFile, HttpServletRequest httpServletRequest) throws IOException{
        printQueryString(logger , httpServletRequest);

        printParams(logger , httpServletRequest);

        printHeaders(logger , httpServletRequest);

        printMultiPart(logger , multipartFile);

        return "success";
    }
    @RequestMapping("withParam")
    public String withParam(@RequestParam(value = "file")MultipartFile multipartFile,@RequestParam MultiValueMap<String, String> parameters , HttpServletRequest httpServletRequest) throws IOException{
        printQueryString(logger , httpServletRequest);

        printParams(logger , httpServletRequest);

        printHeaders(logger , httpServletRequest);

        printMultiPart(logger , multipartFile);

        parameters.forEach((k,v)->logger.info(k + "-------" + v));

        return "success";
    }
}
